<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .a{
            width: 100px;
            height: 100px;
            background-color: #f00;
            position: absolute;
        }
    </style>
</head>
<body>

    <div class="a"></div>

    <script>

        var oA = document.querySelector('.a') ;

        // 每隔一段时间向前移动 , 移动到某个位置就停下来
        //   

        // 获取初始值
        var x = oA.offsetLeft ;
        var speed = 5 ;
        var t = setInterval(function() {
            x += speed ;
            oA.style.left = x + 'px' ;
            if(x >= 1000) {
                // 路程不一定被速度整除   必须到达终点
                x = 1000 ;
                oA.style.left = x + 'px' ;
                clearInterval(t) ;
                
            }
        } , 2)


    </script>
    
</body>
</html>